package com.hotel.front.controller.person;

import com.hotel.comm.bean.*;
import com.hotel.comm.bean.EnumConstants.*;
import com.hotel.comm.bean.lucene.IndexableBean;
import com.hotel.comm.bean.system.EjobCity;
import com.hotel.comm.bean.system.EjobIndustry;
import com.hotel.comm.bean.system.EjobIndustryClass;
import com.hotel.comm.service.search.ILuceneSearchService;
import com.hotel.comm.service.system.ISystemService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/person")
public class PersonSearchController {
    @Autowired
    private ILuceneSearchService luceneSearchService;
    @Autowired
    private ISystemService systemService;

    @RequestMapping(value = "/test.do")
    public String test(
            HttpServletRequest request, HttpServletResponse response) {
        System.out.println("-------------------------------");
        return "jobsearch";
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/search.do")
    public String searchPosition(@RequestParam(value = "cityId", defaultValue = "") String cityId,            //地市Id
                                 @RequestParam(value = "positionName", defaultValue = "") String positionName,        //职位名称
                                 @RequestParam(value = "positionType", defaultValue = "") String positionType,        //职位类型
                                 @RequestParam(value = "workingLife", defaultValue = "") String workingLife,            //工作年限
                                 @RequestParam(value = "salaryRange", defaultValue = "") String salaryRange,            //薪资范围
                                 @RequestParam(value = "companyName", defaultValue = "") String companyName,            //公司名称
                                 @RequestParam(value = "companyNature", defaultValue = "") String companyNature,        //公司性质
                                 @RequestParam(value = "companySize", defaultValue = "") String companySize,            //公司规模
                                 @RequestParam(value = "companyScore", defaultValue = "") String companyScore,        //公司得分
                                 @RequestParam(value = "educationRequirements", defaultValue = "") String educationRequirements,    //学历要求
                                 @RequestParam(value = "currentPage", defaultValue = "1") String currentPage,            //当前分页数
                                 @RequestParam(value = "pageSize", defaultValue = "20") String pageSize,                //分页条数
                                 HttpServletRequest request, HttpServletResponse response) {
        positionName = "工程师";
        IReply reply = this.luceneSearchService.searchPosition(cityId, positionName, positionType, workingLife, salaryRange,
                companyName, companyNature, companySize, companyScore, educationRequirements,
                Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        if (reply.isSuccess()) {
            PageConfig<IndexableBean> pageConfig = (PageConfig<IndexableBean>) ((DubboReply) reply).getT();
            if (pageConfig != null) {
                List<IndexableBean> indexableBeans = pageConfig.getItems();
                if (CollectionUtils.isNotEmpty(indexableBeans)) {
                    for (IndexableBean indexableBean : indexableBeans) {
                        System.out.println(indexableBean);
                    }
                }
                request.setAttribute("pageConfig", pageConfig);
            }
        }
        List<EnumDisplayBean> enums = this.getPositionEnums();
        EnumDisplayBean industry = this.getIndustry();
        enums.add(industry);
        request.setAttribute("positionEnums", enums);
        reply = this.systemService.findAllTopCitys();
        if (reply.isSuccess()) {
            List<EjobCity> topCitys = (List<EjobCity>) ((DubboReply) reply).getCollection();
            request.setAttribute("topCitys", topCitys);
        }
        reply = this.systemService.findAllNonTopCitys();
        if (reply.isSuccess()) {
            Map<String, List<EjobCity>> nontopCityMap = (Map<String, List<EjobCity>>) ((DubboReply) reply).getT();
            request.setAttribute("nontopCityMap", nontopCityMap);
        }
        return "jobsearch";
    }

    private EnumDisplayBean getIndustry() {
        List<EjobIndustryClass> industryClasses = this.systemService.findAllIndustryClass();
        EnumDisplayBean displayBean = null;
        if (CollectionUtils.isNotEmpty(industryClasses)) {
            List<EnumBean> _industryClasses = new ArrayList<EnumBean>();
            for (EjobIndustryClass industryClass : industryClasses) {
                List<EnumBean> industrys = new ArrayList<EnumBean>();
                for (EjobIndustry industry : industryClass.getEjobIndustryList()) {
                    EnumBean _industry = new EnumBean("industryName", industry.getIndustryName(), industry.getIndustryName());
                    industrys.add(_industry);
                }
                _industryClasses.add(new EnumBean("", industryClass.getIndustryClassName(), industryClass.getIndustryClassName(), industrys));
            }
            displayBean = new EnumDisplayBean("行业分类", _industryClasses);
        }
        return displayBean;
    }

    private List<EnumDisplayBean> getPositionEnums() {
        List<EnumDisplayBean> result = new ArrayList<EnumDisplayBean>();
        result.add(CompanyScore.getEnums());
        result.add(WorkingLife.getEnums());
        result.add(SalaryRange.getEnums());
        result.add(EducationRequirements.getEnums());
        result.add(CompanyNature.getEnums());
        return result;
    }
}
